import time
from Common.Utils import WebDriver
from Test.Login import login
from selenium.webdriver.common.by import By


class Stu:
    def __init__(self):
        self.url = "http://localhost:8080/#/login?redirect=%2Fdashboard"
        self.driver = WebDriver.driver
        self.driver.get(self.url)
        self.driver.implicitly_wait(3)

    # 登录工作
    def Login(self, user, password):
        login.Init(user, password)
        login.autoIdentifyCode()
        time.sleep(7)
        self.driver.find_element(By.CSS_SELECTOR, "#app > div > form > button").click()
        # 点击学生管理页面
        self.driver.find_element(By.CSS_SELECTOR,
                                 '#app > div > div.sidebar-container.has-logo > div.el-scrollbar > div.scrollbar-wrapper.el-scrollbar__wrap > div > ul > div:nth-child(2) > a > li').click()

    # self.driver.find_element(By.CSS_SELECTOR, '').click()
    # 添加新的学生, 不填写参数
    def AddFail(self):
        self.Login('admin', 'admin')
        self.driver.find_element(By.CSS_SELECTOR,
                                 '#app > div > div.main-container > section > div > div > div:nth-child(1) > form > div:nth-child(1) > div > button').click()
        self.driver.find_element(By.CSS_SELECTOR,
                                 '#app > div > div.main-container > section > div > div > div.el-dialog__wrapper > div > div.el-dialog__footer > div > button.el-button.el-button--primary').click()
        time.sleep(0.5)
        self.driver.save_screenshot('../images/student/student1.png')

    # 正确填写参数添加
    def AddSuc(self, path='../images/student/student2.png'):
        self.driver.find_element(By.CSS_SELECTOR,
                                 '#app > div > div.main-container > section > div > div > div.el-dialog__wrapper > div > div.el-dialog__body > form > div.el-form-item.is-error.is-required > div > div.el-input > input').send_keys(
            'aaa')
        self.driver.find_element(By.CSS_SELECTOR,
                                 '#app > div > div.main-container > section > div > div > div.el-dialog__wrapper > div > div.el-dialog__body > form > div:nth-child(4) > div > div.el-input > input').send_keys(
            '100')
        self.driver.find_element(By.CSS_SELECTOR,
                                 '#app > div > div.main-container > section > div > div > div.el-dialog__wrapper > div > div.el-dialog__body > form > div:nth-child(5) > div > div > input').send_keys(
            '123456')
        self.driver.find_element(By.CSS_SELECTOR,
                                 '#app > div > div.main-container > section > div > div > div.el-dialog__wrapper > div > div.el-dialog__body > form > div:nth-child(6) > div > div > input').send_keys(
            '123456')
        self.driver.find_element(By.CSS_SELECTOR,
                                 '#app > div > div.main-container > section > div > div > div.el-dialog__wrapper > div > div.el-dialog__body > form > div:nth-child(7) > div > div > input').send_keys(
            'aaa@qq.com')
        self.driver.find_element(By.CSS_SELECTOR,
                                 '#app > div > div.main-container > section > div > div > div.el-dialog__wrapper > div > div.el-dialog__body > form > div:nth-child(8) > div > div > input').send_keys(
            '13899999999')
        self.driver.find_element(By.CSS_SELECTOR,
                                 '#app > div > div.main-container > section > div > div > div.el-dialog__wrapper > div > div.el-dialog__body > form > div:nth-child(9) > div > div > input').send_keys(
            '洛阳')
        self.driver.find_element(By.CSS_SELECTOR,
                                 '#app > div > div.main-container > section > div > div > div.el-dialog__wrapper > div > div.el-dialog__body > form > div:nth-child(10) > div > div > textarea').send_keys(
            'aaaaaaaaaa')

        # 手动选择班级、性别和头像（动态id无法锁定）
        time.sleep(15)

        self.driver.find_element(By.CSS_SELECTOR,
                                 '#app > div > div.main-container > section > div > div > div.el-dialog__wrapper > div > div.el-dialog__footer > div > button.el-button.el-button--primary > span').click()
        time.sleep(0.5)
        self.driver.save_screenshot(path)

    # 重复添加, 再次调用AddSuc函数, 使用缺省参数改变图片路径
    def AddFail2(self):
        # 激活文本框，不然这里会被锁定，虽然我也不知道怎么会这样
        self.driver.find_element(By.CSS_SELECTOR, '#app > div > div.main-container > section > div > div > div:nth-child(1) > form > div:nth-child(1) > div > button > span').click()

        self.driver.find_element(By.CSS_SELECTOR,
                                 '#app > div > div.main-container > section > div > div > div.el-dialog__wrapper > div > div.el-dialog__footer > div > button.el-button.el-button--primary').click()

        self.AddSuc('../images/student/student3.png')

    # 从班级开始查询(二年二班)
    def SelectClass(self):
        self.driver.find_element(By.CSS_SELECTOR,
                                 '#app > div > div.main-container > section > div > div > div:nth-child(1) > form > div:nth-child(2) > div > div > div > input').click()
        # 手动选择, 规避动态ID问题
        time.sleep(3)
        self.driver.find_element(By.CSS_SELECTOR,
                                 '#app > div > div.main-container > section > div > div > div:nth-child(1) > form > div:nth-child(4) > div > button.el-button.el-button--primary.el-button--small > span').click()
        time.sleep(0.5)
        self.driver.save_screenshot('../images/student/student4.png')

    # 重置查询
    def ReSet(self):
        self.driver.find_element(By.CSS_SELECTOR,
                                 '#app > div > div.main-container > section > div > div > div:nth-child(1) > form > div:nth-child(4) > div > button.el-button.el-button--info.el-button--small').click()
        time.sleep(0.5)
        self.driver.save_screenshot('../images/student/student5.png')

    # 学生查询(aaa)
    def SelectStudent(self):
        self.driver.find_element(By.CSS_SELECTOR,
                                 '#app > div > div.main-container > section > div > div > div:nth-child(1) > form > div:nth-child(3) > div > div > input').clear()
        self.driver.find_element(By.CSS_SELECTOR,
                                 '#app > div > div.main-container > section > div > div > div:nth-child(1) > form > div:nth-child(3) > div > div > input').send_keys(
            'aaa')
        self.driver.find_element(By.CSS_SELECTOR,
                                 '#app > div > div.main-container > section > div > div > div:nth-child(1) > form > div:nth-child(4) > div > button.el-button.el-button--primary.el-button--small > span').click()
        time.sleep(0.5)
        self.driver.save_screenshot('../images/student/student6.png')

    # 修改六年一班信息,改为六年二班, 但是空填学号
    def Modify(self):
        time.sleep(7)  # 手动选择aaa
        self.driver.find_element(By.CSS_SELECTOR,
                                 '#app > div > div.main-container > section > div > div > div.el-dialog__wrapper > div > div.el-dialog__body > form > div:nth-child(4) > div > div > input').clear()

        # 手动清空学号
        time.sleep(5)
        self.driver.find_element(By.CSS_SELECTOR,
                                 '#app > div > div.main-container > section > div > div > div.el-dialog__wrapper > div > div.el-dialog__body > form > div:nth-child(4) > div > div > input').click()

        time.sleep(0.5)
        self.driver.save_screenshot('../images/student/student7.png')
        self.driver.find_element(By.CSS_SELECTOR,
                                 '#app > div > div.main-container > section > div > div > div.el-dialog__wrapper > div > div.el-dialog__body > form > div:nth-child(4) > div > div > input').send_keys(
            '101')
        self.driver.find_element(By.CSS_SELECTOR,
                                 '#app > div > div.main-container > section > div > div > div.el-dialog__wrapper > div > div.el-dialog__footer > div > button.el-button.el-button--primary').click()
        time.sleep(0.5)
        self.driver.save_screenshot('../images/student/student8.png')

    # 删除(Delete) 需动态手工测试


# stu = Stu()
# stu.AddFail()
# stu.AddSuc()
# stu.AddFail2()
# stu.SelectClass()
# stu.ReSet()
# stu.SelectStudent()
# stu.Modify()
